import { HttpClient, HttpParams } from "@angular/common/http";
import { Injectable } from "@angular/core";
import { Observable } from "rxjs";
import { Student, StudentQuery, StudentResponse, StudentType } from "../types/student";


@Injectable({
    providedIn: 'root'
})
export class StudentService{
    private baseUrl = 'http://localhost:8080/students';

    constructor(private http: HttpClient){}


    getStudents(query?: StudentQuery): Observable<StudentResponse<StudentType>> {
        let params = new HttpParams();
        if (query) {
          if (query.page) params = params.set('page', query.page.toString());
          if (query.page_size) params = params.set('page_size', query.page_size.toString());
          if (query.class) params = params.set('class', query.class);
        }
        return this.http.get<StudentResponse<StudentType>>(this.baseUrl, { params });
      }
    
      getStudent(id: number): Observable<Student> {
        return this.http.get<Student>(`${this.baseUrl}/${id}`);
      }
    
      createStudent(Student: Student): Observable<Student> {
        return this.http.post<Student>(this.baseUrl, Student);
      }
    
      updateStudent(id: number, Student: Student): Observable<Student> {
        return this.http.put<Student>(`${this.baseUrl}/${id}`, Student);
      }
    
      deleteStudent(id: number): Observable<void> {
        return this.http.delete<void>(`${this.baseUrl}/${id}`);
      }


}